package com.happy.study.controller;

import com.happy.study.entity.CollectionEntity;
import com.happy.study.entity.PaperEntity;
import com.happy.study.entity.QuestionEntity;
import com.happy.study.entity.UserEntity;
import com.happy.study.entity.bo.CollectionBo;
import com.happy.study.servier.ICollectionService;
import com.happy.study.servier.IPaperService;
import com.happy.study.servier.IQuestionService;
import com.happy.study.util.ResultBody;
import com.happy.study.util.StateEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping ("/collection")
public class CollectionController {

    @Resource
    private ICollectionService iCollectionService;
    @Resource
    private IQuestionService iQuestionService;
    @Resource
    private IPaperService iPaperService;

    @RequestMapping ("/addSingle")
    @ResponseBody
    public ResultBody<CollectionEntity> addSingle ( CollectionEntity collectionEntity, HttpSession session ) {
        UserEntity userEntity = (UserEntity) session.getAttribute ( "userEntity" );
        if ( userEntity == null ) {
            return ResultBody.fail ( StateEnum.REQUEST_POWER_EXCE );
        }
        collectionEntity.setUserId ( userEntity.getId () );
        collectionEntity = iCollectionService.addSingle ( collectionEntity );
        return ResultBody.success ( collectionEntity );
    }

    @RequestMapping ("/queryList")
    @ResponseBody
    public ResultBody<CollectionBo> queryList ( CollectionEntity collectionEntity ) {
        List<CollectionEntity> collectionEntities = iCollectionService.queryList ( collectionEntity );
        List<CollectionBo> collectionBos = new ArrayList<> ();
        for ( CollectionEntity collection : collectionEntities ) {
            if ( collection.getType () == 1 ) {
                QuestionEntity questionEntity = new QuestionEntity ();
                questionEntity.setId ( collection.getRelationId () );
                questionEntity = iQuestionService.querySingle ( collection.getRelationId () );
                CollectionBo collectionBo = new CollectionBo ();
                collectionBo.setTitle ( questionEntity.getTitle () );
                collectionBo.setContent ( questionEntity.getSubTitle () );
                collectionBo.setCreateTime ( questionEntity.getCreateTime () );
                collectionBo.setType ( collection.getType () );
                collectionBos.add ( collectionBo );
            } else if ( collection.getType () == 2 ) {
                PaperEntity paperEntity = new PaperEntity ();
                paperEntity.setPaperId ( collection.getRelationId () );
                paperEntity = iPaperService.querySingle ( collection.getRelationId () );
                CollectionBo collectionBo = new CollectionBo ();
                collectionBo.setTitle ( paperEntity.getPaperName () );
                collectionBo.setLevel ( paperEntity.getLevel () );
                collectionBo.setCreateTime ( paperEntity.getCreateTime () );
                collectionBo.setType ( collection.getType () );
                collectionBos.add ( collectionBo );
            }
        }
        return ResultBody.success ( collectionBos, collectionBos.size () );
    }


}
